package eleven.senven.hpb.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.etsy.android.grid.util.DynamicHeightImageView;
import com.lidroid.xutils.BitmapUtils;

import java.util.List;
import java.util.Random;

import eleven.senven.hpb.R;
import eleven.senven.hpb.bean.OslankaBean01;

/**
 * Created by cnn on 2015/7/13.
 */
public class OslankaAdapter01 extends MyBaseAdapter<OslankaBean01.ResultEntity> {
    private BitmapUtils bitmapUtils;
    private final Random mRandom;
    public OslankaAdapter01(List<OslankaBean01.ResultEntity> datas, Context context,BitmapUtils bitmapUtils) {
        super(datas, context);
        this.bitmapUtils = bitmapUtils;
        mRandom = new Random();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            convertView=inflater.inflate(R.layout.oslanka_grid_item,parent,false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        double positionHeight = getPositionRatio(position);
        viewHolder.imageView.setHeightRatio(positionHeight);
        OslankaBean01.ResultEntity bean = datas.get(position);

        bitmapUtils.display(viewHolder.imageView, bean.getPicture_url());
        viewHolder.name.setText(bean.getFood_name());
        viewHolder.price.setText(
                ""+bean.getFood_price());
        viewHolder.content.setText(bean.getComment());
        bitmapUtils.display(viewHolder.image_logo, bean.getUser_image());
        viewHolder.username.setText(bean.getUser_name());
        viewHolder.contentuser.setText(bean.getPlace_name());
        viewHolder.phone.setText(bean.getPhone());

        return convertView;
    }
    static class   ViewHolder{
        DynamicHeightImageView imageView ;
        TextView name,price,content,username,contentuser,phone;
        ImageView image_logo;
        public ViewHolder(View itemview) {
            imageView = (DynamicHeightImageView) itemview.findViewById(R.id.image);
            name = (TextView) itemview.findViewById(R.id.name);
            price = (TextView) itemview.findViewById(R.id.price);
            content = (TextView) itemview.findViewById(R.id.content);
            username = (TextView) itemview.findViewById(R.id.username);
            contentuser = (TextView) itemview.findViewById(R.id.contentuser);
            phone = (TextView) itemview.findViewById(R.id.phone);
            image_logo = (ImageView) itemview.findViewById(R.id.image_logo);
        }
    }
    private static final SparseArray<Double> sPositionHeightRatios = new SparseArray<Double>();
    private double getPositionRatio(final int position) {
        double ratio = sPositionHeightRatios.get(position, 0.0);
        if (ratio == 0) {
            ratio = getRandomHeightRatio();
            sPositionHeightRatios.append(position, ratio);
        }
        return ratio;
    }
    private double getRandomHeightRatio() {
        return (mRandom.nextDouble() / 2.0) + 1.0; // height will be 1.0 - 1.5 the width
    }


}
